package at.spengergasse.abifs.SDJPATest.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import at.spengergasse.abifs.SDJPATest.repository.TrainingGroupRepository;


import at.spengergasse.abifs.SDJPATest.model.Athlet;
import at.spengergasse.abifs.SDJPATest.model.Coach;
import at.spengergasse.abifs.SDJPATest.model.TrainingGroup;

@Service("trainingGroupService")
public class TrainingGroupServiceImpl implements TrainingGroupService
{

	@Autowired
	private TrainingGroupRepository trainingGroupRepository;
	
	@PersistenceContext
    private EntityManager em;
	
	@Override
	@Transactional
	public void save(TrainingGroup tg) 
	{
		trainingGroupRepository.save(tg);		
	}

	@Override
	@Transactional
	public void delete(TrainingGroup tg) 
	{
		trainingGroupRepository.delete(tg);
	}

	@Override
	@Transactional(readOnly = true)
	public TrainingGroup findByID(Long tg_id) 
	{
		return trainingGroupRepository.findByID(tg_id);
	}

	@Override
	@Transactional(readOnly = true)
	public List<TrainingGroup> findAllTrainingGroups() 
	{
		return trainingGroupRepository.findAll();
	}

	@Override
	public List<TrainingGroup> findByCoach(Coach coach) 
	{
		TypedQuery<TrainingGroup> query = em.createQuery("select tg from TrainingGroup tg where tg.coach = ?1", TrainingGroup.class);
		query.setParameter(1, coach);
		return query.getResultList();
	}

}
